package com.example.office.util;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @Author jujh
 * @Date 2018/8/10
 * java后端根据经纬度获取地理位置
 * x纬度 y经度
 */
public class AddressParse {
    /**
     * 正向地址匹配接口
     * 根据xy获取所在省市县
     *
     * @param xy
     * @return
     */
    public static String geodecode(String xy) {
        String sUrl = "http://api.map.baidu.com/geocoder/v2/?ak=MLy2Xs1D6csnuwORcntVO6y8swoPnQ8R&location=" + xy + "&output=json&pois=0";
        StringBuffer str = new StringBuffer();
        String address = "";
        try {
            java.net.URL url = new java.net.URL(sUrl);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                str.append(line);
            }
            in.close();
            if (str.equals("") || str == null) {
                System.err.println("百度服务无返回");
                return null;
            }
            JSONObject jsonobj = JSONObject.fromObject(str.toString());
            JSONObject result = jsonobj.getJSONObject("result");// 获取对象
            address = result.get("formatted_address") + "";
            System.out.println("坐标对应的位置=" + address);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address;
    }

    public static void main(String[] args) {
        // String address = AddressParse.geodecode("30.655200999999998,104.263393");
        // System.out.println(address);
        String json = "[{\"accStatus\":1,\"altitude\":1057,\"dataType\":\"CSTDP0007\",\"dataValid\":true,\"din\":\"M503817103092\",\"gpsMode\":\n" +
                "1,\"gpsSpeed\":21,\"gpsTimestamp\":1534412502,\"heading\":105,\"latitude\":40.058040999999996,\"longitude\":113.27782499999999,\"msgId\":1030026819090436096,\"obdTime\":1534412503000,\"openCarId\":\"776f0fees54d24887955fPb7U3UcG782aGG25f5H\",\"openId\":\"M503817103092\",\"satellites\":6,\"speed\":21,\"totalDistance\":1088.245,\"totalDistanceType\":1,\"totalFuel\":117.139},{\"accStatus\":1,\"altitude\":1058,\"dataType\":\"CSTDP0007\",\"dataValid\":true,\"din\":\"M503817103092\",\"gpsMode\":1,\"gpsSpeed\":22.3,\"gpsTimestamp\":1534412503,\"heading\":123,\"latitude\":40.058012999999995,\"longitude\":113.277885,\"msgId\":1030026825520304129,\"obdTime\":1534412504000,\"openCarId\":\"776f0fees54d24887955fPb7U3UcG782aGG25f5H\",\"openId\":\"M503817103092\",\"satellites\":7,\"speed\":23,\"totalDistance\":1088.253,\"totalDistanceType\":1,\"totalFuel\":117.139},{\"accStatus\":1,\"altitude\":1058,\"dataType\":\"CSTDP0007\",\"dataValid\":true,\"din\":\"M503817103092\",\"gpsMode\":1,\"gpsSpeed\":24.1,\"gpsTimestamp\":1534412504,\"heading\":140,\"latitude\":40.057970999999995,\"longitude\":113.277935,\"msgId\":1030026825520304130,\"obdTime\":1534412505000,\"openCarId\":\"776f0fees54d24887955fPb7U3UcG782aGG25f5H\",\"openId\":\"M503817103092\",\"satellites\":7,\"speed\":26,\"totalDistance\":1088.259,\"totalDistanceType\":1,\"totalFuel\":117.14},{\"accStatus\":1,\"altitude\":1057,\"dataType\":\"CSTDP0007\",\"dataValid\":true,\"din\":\"M503817103092\",\"gpsMode\":1,\"gpsSpeed\":26,\"gpsTimestamp\":1534412505,\"heading\":156,\"latitude\":40.057915,\"longitude\":113.277968,\"msgId\":1030026825524498432,\"obdTime\":1534412506000,\"openCarId\":\"776f0fees54d24887955fPb7U3UcG782aGG25f5H\",\"openId\":\"M503817103092\",\"satellites\":7,\"speed\":28,\"totalDistance\":1088.266,\"totalDistanceType\":1,\"totalFuel\":117.142}]";
        System.out.println(json.substring(0, 1).substring(json.length()-1,json.length()));
        JSONArray jsonArray = JSONArray.fromObject(json);
        System.out.println(jsonArray.size());
        JSONObject carjson = jsonArray.getJSONObject(0);
        System.out.println(carjson);


    }
}
